package com.test.flink_transforms;

import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.util.Collector;

/**
 * 接受一个数据 返回多个结果数据
 */
public class F02FlatMap {

    public static void main(String[] args) throws Exception {
        Configuration configuration = new Configuration();
        configuration.setInteger("rest.port", 8888);
        StreamExecutionEnvironment webUI = StreamExecutionEnvironment.createLocalEnvironmentWithWebUI(configuration);

        DataStream<String> data = webUI.socketTextStream("127.0.0.1", 8899);

        DataStream<String> res = data.flatMap(new FlatMapFunction<String, String>() {
            @Override
            public void flatMap(String s, Collector<String> out) throws Exception {
                String[] words = s.split("\\s+");

                for (String word : words) {
                    out.collect(word);
                }
            }
        });

        res.print();

        webUI.execute();

    }
}
